﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sinacor.Infra.Core.Licensing;

namespace LicensingTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Licensing CriaLic()
        {
            // Cria objeto de licença
            Licensing licensing = new Licensing();

            // Define informações da licença
            licensing.OwnerLegalRegistrationId = new Company("Banco de Investimentos sem Retorno S/A", "03.938.970/0001-06", 0).LegalRegistrationId;

            // Licença válida por 60 dias
            // A data de criação é gerada automaticamente
            //licensing.GoodThru = licensing.Created.Value.AddDays(1);

            // Define quais CNPJs estão habilitados para essa licença
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));
            licensing.Companies.Add(new Company("Fazendas Reunidas Boi mais Gordo ainda Ltda", "01.137.487/0021-56", 1));
            licensing.Companies.Add(new Company("Teste 001 Ltda", "06.512.389/0001-14", 2));
            licensing.Companies.Add(new Company("Teste 004544200 Ltda", "02.831.865/0002-03", 3));
            licensing.Companies.Add(new Company("Teste 035465487685340 Ltda", "03.422.032/0121-16", 99));

            // Define o tipo de licença que será tratada
            licensing.Treaty = Treaty.Modulo;
            
            // Como o tipo é módulo, define quais módulos estão liberados para essa licença;
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Ordens BMF", "OMF", false, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Conta Corrente", "CCO", true, licensing.Created.Value.AddDays(1)));
            licensing.Modules.Add(new Module("Kernel+", "KNL", true, licensing.Created.Value.AddDays(1)));

            return licensing;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            // Cria Gerador de Licença
            LicensingGenerator licensingGenerator = new LicensingGenerator();

            // Cria uma licença de teste
            Licensing licensing = CriaLic();

            // Gera uma licença assinada;
            SignedLicensing signedLicensing = licensingGenerator.Generate(licensing);

            // Grava os arquivos de licença
            signedLicensing.SaveLicensingInFiles(@"C:\licenca.lic", @"C:\licenca.key", @"C:\licenca.ivkey", @"C:\licenca.publickey", @"C:\licenca.sig");
        }

        private void btnValidade_Click(object sender, EventArgs e)
        {
            // Cria o objeto de licença assinada
            SignedLicensing signedLicensing = new SignedLicensing();

            // Carrega a licença a partir dos arquivos descritos
            signedLicensing.LoadLicensingFromFiles(@"C:\licenca.lic", @"C:\licenca.key", @"C:\licenca.ivkey", @"C:\licenca.publickey", @"C:\licenca.sig");

            // Cria o objeto que valida licença
            LicensingValidator licensingValidator = new LicensingValidator();

            try
            {
                // Define a licença assinada a ser validada
                licensingValidator.SignedLicensing = signedLicensing;
            }
            catch (LicensingExpiredException leexc)
            {
                MessageBox.Show(leexc.Message);
            }
            catch (LicensingCorruptedException lcexc)
            {
                MessageBox.Show(lcexc.Message);
            }

            // Se estiver válida 
            if (licensingValidator.IsValid)
                MessageBox.Show("Licença Válida");
            else // Se estiver inválida
                MessageBox.Show("Licença Inválida");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Cria Gerador de Licença
            LicensingGenerator licensingGenerator = new LicensingGenerator();

            // Cria uma licença de teste
            Licensing licensing = CriaLic();

            // Gera uma licença assinada;
            SignedLicensing signedLicensing = licensingGenerator.Generate(licensing);

            // Grava os arquivos de licença
            signedLicensing.SaveMappedLicensingFile(@"C:\Sinacor.lic");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Cria o objeto de licença assinada
            SignedLicensing signedLicensing = new SignedLicensing();

            // Carrega a licença a partir dos arquivos descritos
            signedLicensing.LoadMappedLicensingFile(@"C:\Sinacor.lic");

            // Cria o objeto que valida licença
            LicensingValidator licensingValidator = new LicensingValidator();

            try
            {
                // Define a licença assinada a ser validada
                licensingValidator.SignedLicensing = signedLicensing;
            }
            catch (LicensingExpiredException leexc)
            {
                MessageBox.Show(leexc.Message);
            }
            catch (LicensingCorruptedException lcexc)
            {
                MessageBox.Show(lcexc.Message);
            }

            // Se estiver válida 
            if (licensingValidator.IsValid)
                MessageBox.Show("Licença Válida");
            else // Se estiver inválida
                MessageBox.Show("Licença Inválida");

        }


    }
}
